let cartApp=new Vue({
    el:"#cartApp",
    data:{
        cart:{}
    },
    methods:{
        loadCart:function () {
            this.$forceUpdate();
            axios({
                url:"/carts/my",
                method:"get"
            }).then(function (response) {
                cartApp.cart=response.data;
            })
        },
        deleteItems:function (itemsId,index) {
            axios({
                url: "/carts/delete/"+itemsId,
                method: "get"
            }).then(function (response) {
                if (response.data == "ok"){
                    cartApp.loadCart();
                }
            })
        },
        //数量+1
        addCount:function(itemsId,quantity,index){
            let count=quantity+1
            axios({
                url:"/carts/update/"+itemsId+"/"+count,
                method:"get"
            }).then(function (response) {
                if (response.data == "ok"){
                    cartApp.loadCart();
                }else {
                    alert(response.data);
                }
            })
        },
        //数量-1
        reduceCount:function (itemsId,quantity,index) {
            let count=quantity-1
            if (count <=0){
                alert("数量不能小于等于0")
                return;
            }
            axios({
                url:"/carts/update/"+itemsId+"/"+count,
                method:"get"
            }).then(function (response) {
                if (response.data == "ok"){
                    cartApp.loadCart();
                }else {
                    alert(response.data);
                }
            })
        },
        //直接修改数量
        inputCount:function(itemsId){
            let quantity=$(".cart-quantity"+itemsId).val();
            axios({
                url:"/carts/update/"+itemsId+"/"+quantity,
                method:"get"
            }).then(function (response) {
                if (response.data == "ok"){
                    cartApp.loadCart();
                }else {
                    alert(response.data);
                }
            })
        }

    },
    created:function () {
        this.loadCart();
    }
})